import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
// https://vitejs.dev/config/
export default defineConfig({
  server: {
    open: true,
    port: 9421,
    proxy: {
      // 字符串匹配
      '/api': {
        target: 'https://xxxx/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    },
  },
  plugins: [
    vue(),
  ],
  // 别名
  resolve: {
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'], // 导入时想要省略的扩展名列表
    // 别名
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@import "@/styles/mixin.scss";`,
      },
    },
    postcss: {
      plugins: [
        /**
         * 修复无法打包的问题 import 'element-plus/dist/index.css'
         * 报错：rendering chunks (3)...[!] Error: unfinished hook action(s) on exit:(vite:css-post) renderChunk
         */
        {
          postcssPlugin: 'internal:charset-removal',
          AtRule: {
            charset: (atRule) => {
              if (atRule.name === 'charset') {
                atRule.remove()
              }
            }
          }
        }
      ]
    }
  },
  build: {
    // cssCodeSplit: false, // 启用/禁用 CSS 代码拆分 禁用后 css拆成独立文件
    sourcemap: false, // 是否打包sourcemap
  },
})
